﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using System.Transactions;
using MyException;

namespace BusinessLogicLayer
{
    public class SupplierBizlogic
    {
        SupplierDataAccess sr;
        CategoryRepository categoryRepo;
        StationeryRepository stationeryRepo;

        public SupplierBizlogic()
        {
            sr = new SupplierDataAccess();
            categoryRepo = new CategoryRepository();
            stationeryRepo = new StationeryRepository();
        }

        public List<string> getDepId()
        {
            List<string> idlist = new List<string>();
            idlist = sr.getSupplierid();
            return idlist;

        }
        public bool savesupplier(String SupplierID, String Suppliername, String ContactName, int Phoneno, int Faxno, String GSTregno, String address, String email)
        {
            bool ans = false;
            ans = sr.savesupplier(SupplierID, Suppliername, ContactName, Phoneno, Faxno, GSTregno, address, email);

            return ans;
        }

        public void SaveNewSupplier(Supplier supplier)
        {
            sr.saveNewSupplier(supplier);
        }

        public List<object> ret_sta_items_all()
        {
            List<object> rst = new List<object>();
            rst = sr.category_searchall();
            return rst;
        }
        public List<object> ret_sta_items(int search)
        {
            List<object> list_items = new List<object>();
            list_items = sr.category_search(search);
            return list_items;
        }
        public List<object> ret_sta_items(string search)
        {
            List<object> list_items = new List<object>();
            list_items = sr.category_search(search);
            return list_items;
        }

        public Stationery getStationery(string itemCode)
        {
            Stationery stationery = new Stationery();
            stationery = sr.getStationery(itemCode);
            return stationery;
        }

        public List<SupplierDetail> getSupplierDetailByItemCode(string itemCode)
        {
            List<SupplierDetail> list = new List<SupplierDetail>();
            list = sr.getSupplierDetailByItemCode(itemCode);
            return list;
        }

        public List<StockHistory> getStockHistoryByItemCode(string itemCode)
        {
            List<StockHistory> list = new List<StockHistory>();
            list = sr.getStockHistoryByItemCode(itemCode);
            return list;
        }

        public List<StockHistory> getStockHistoryByDate(List<StockHistory> stockHistory, DateTime startDate, DateTime endDate)
        {
            List<StockHistory> list = new List<StockHistory>();
            list = sr.getStockHistoryByDate(stockHistory, startDate, endDate);
            return list;
        }

        public List<object> getcategories()
        {
            List<object> categories = new List<object>();
            categories = sr.getcategory();
            return categories;

        }
        public List<object> getCatItems(int category)
        {
            List<object> catItems = new List<object>();
            catItems = sr.getCatItems(category);


            return catItems;
        }
        public List<Object> getsuppliers()
        {
            List<Object> supplist = new List<object>();
            supplist = sr.getSuppliers();
            return supplist;

        }

        public void sendnotification(int empID, String userName,String status,String CollectionPoint, DateTime CollectionDate, DateTime CollectionTime)
        {
            SupplierDataAccess sad = new SupplierDataAccess();
            Employee emp = sad.getemployee(empID);
            CollectionPointDetail coll_time = sad.getcollectiontime(CollectionTime);
            CollectionPointDetail coll_date = sad.getcollectiondate(CollectionDate);


            String Subject, Body;

            String Email = emp.Email;
            if (status == "Updated")
            {
                Subject = "Collection Point changed";
                Body = "Dear " + emp.EmployeeName + ",\nThis is to inform you that the collection date" + coll_date.CollectionDate.ToShortDateString() + "and collection time" + coll_time.CollectionTime.ToShortTimeString() + "for the collection point " +
                    CollectionPoint + "has been changed" + ".\n" +
                    "\nThank you!";
                EmailNotificationControl email = new EmailNotificationControl();
                email.sendNotification("ada_rshcool@yahoo.com","chsarpteam3@gmail.com",Body,Subject);//Email

            }
        }

        public List<Category> DisplayAllCategory()
        {
            List<Category> list = new List<Category>();
            // Add all
            Category category = new Category();
            category.CategoryID = 0;
            category.CategoryName = "All";
            list.Add(category);
            // Add categories
            List<Category> categoryList = categoryRepo.GetAllCategory();
            foreach (Category cat in categoryList)
            {
                list.Add(cat);
            }                                                
            return list;
        }

        public void AddNewStationeryItem(Stationery stationery, List<SupplierDetail> supDetailList)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                Stationery stat = stationeryRepo.GetStationeryByItemCode(stationery.ItemCode);
                if (stat.ItemCode != null)
                {
                    throw new DuplicateItemCodeException();
                }
                else
                {
                    sr.AddStationery(stationery);

                    for (int i = 0; i < 3; i++)
                    {
                        string tempSupID = supDetailList[i].SupplierID;
                        decimal tempPrice = (decimal)supDetailList[i].Price; 
                        for (int j = i + 1; j < 3; j++)
                        {
                            if (supDetailList[j].SupplierID.Equals(tempSupID))
                            {
                                throw new DuplicateSupplierException();
                            }
                            if ((decimal)supDetailList[j].Price < tempPrice)
                            {
                                
                                throw new SupplierPriceNotInOrderException();
                            }
                        }
                        sr.AddSupplierDetail(supDetailList[i]);
                    }
                    ts.Complete();                    
                }
            }
        }

        public void AddStationery(Stationery stationery)
        {
            sr.AddStationery(stationery);            
        }

        public void AddSupplierDetail(SupplierDetail supplierDetail)
        {
            sr.AddSupplierDetail(supplierDetail);
        }

        public void UpdateStationeryReorderLvlAndQty(string itemCode, int reorderLvl, int reorderQty)
        {
            Stationery stationery = sr.getStationery(itemCode);
            stationery.ReorderLevel = reorderLvl;
            stationery.ReorderQty = reorderQty;
            stationeryRepo.UpdateStationery(stationery);
        }

        public List<object> getSelectedSupplierDetail(string itemCode)
        {
            return sr.getSelectedSupplierDetail(itemCode);
        }

        public void GetSupplierBySupplierId(string supplerId)
        {
            Supplier sup = sr.GetSupplierBySupplierId(supplerId);

            if (sup != null)
            {
                throw new DuplicateSupplierException();
            }
        }
    }
}
